﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz;
using System.Collections.Generic;
using Cn.Cellula.Model.Product;
using Cn.Edwon.Web.Script;

namespace Cn.Cellula.Website.Ws {
    public partial class _ProductInCategory : PageSupport {
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        ICategoryBiz cbiz = BizFactory.Get<ICategoryBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            try {
                LoadProduct();
            } catch (Exception ex) {
                log.Error(ex.Message);
                Response.Clear();
                Response.Write("[]");
            }
        }
        private void LoadProduct() {
            String cfilter = "";
            String cid = Request.QueryString["cid"];
            if (!String.IsNullOrEmpty(cid)) {
                IList<String> ckeys = cbiz.LoadDescendantsKeys(null, CodeTypeDef.Category.Product, cid);
                if (ckeys != null && ckeys.Count > 0) {
                    cfilter += " and ( eo.CategoryId='" + cid + "' ";
                    for (int i = 0; i < ckeys.Count; i++) {
                        String k = ckeys[i];
                        cfilter += " or ";
                        cfilter += " eo.CategoryId='" + k + "'";
                    }
                    cfilter += " ) ";
                } else {
                    cfilter += " and eo.CategoryId='" + cid + "' ";
                }
            }
            String hql = "from VProductDetailsEO eo where eo.ProductId is not null ";
            hql += cfilter;
            hql += " order by eo.LastDate desc ";
            IList<Object> list = biz.Find<Object>(hql);
            String json = JsUtil.ParseEOList2Json(list);
            Response.Clear();
            Response.Write(json);
        }
    }
}
